<script setup>
import { useRouter } from 'vue-router'
import { ref, onUnmounted } from 'vue'

const router = useRouter()

// 倒计时秒数
const countdown = ref(5)
let timer = null
let countdownInterval = null

// 返回首页
const goHome = () => {
  router.push('/')
}

// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 启动倒计时
const startCountdown = () => {
  countdownInterval = setInterval(() => {
    if (countdown.value > 1) {
      countdown.value--
    } else {
      clearInterval(countdownInterval)
      goHome()
    }
  }, 1000)
}

// 组件挂载时启动倒计时
startCountdown()

// 组件卸载时清理定时器
onUnmounted(() => {
  if (countdownInterval) {
    clearInterval(countdownInterval)
  }
})
</script>

<template>
  <div class="not-found-container">
    <div class="error-card">
      <div class="error-code">404</div>
      <van-icon name="warning-o" class="error-icon" />
      <div class="error-title">页面未找到</div>
      <div class="error-description">抱歉，您访问的页面不存在</div>
      <div class="countdown-text">将在 {{ countdown }} 秒后自动返回首页</div>

      <div class="button-group">
        <van-button
          round
          type="primary"
          class="action-button"
          @click="goHome"
        >
          返回首页
        </van-button>
        <van-button
          round
          plain
          type="primary"
          class="action-button"
          @click="goBack"
        >
          返回上一页
        </van-button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.not-found-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 20px;
  background-color: #f7f8fa;
}

.error-card {
  text-align: center;
  background-color: #fff;
  border-radius: 16px;
  padding: 40px 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  width: 90%;
  max-width: 400px;
}

.error-code {
  font-size: 64px;
  font-weight: bold;
  color: #ee0a24;
  margin-bottom: 10px;
  font-family: 'Arial', sans-serif;
}

.error-icon {
  font-size: 48px;
  color: #ee0a24;
  margin: 10px 0;
}

.error-title {
  font-size: 20px;
  font-weight: 600;
  color: #323233;
  margin: 16px 0 8px;
}

.error-description {
  font-size: 14px;
  color: #646566;
  margin-bottom: 20px;
  line-height: 1.5;
}

.countdown-text {
  font-size: 13px;
  color: #969799;
  margin-bottom: 24px;
}

.button-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.action-button {
  width: 100%;
  margin: 0;
}
</style>